获取 input$variable Shiny RStudio 的最大值和最小值 [英] Get maximum and minimum values for input$variable Shiny RStudio

查看:49
本文介绍了获取 input$variable Shiny RStudio 的最大值和最小值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我叫 Nate,我正在尝试使用来自 RStudio 的 Shiny 构建一个小型数据分析工具.

My name is Nate and I am trying to build a little data analysis tool using Shiny from RStudio.

现在我正在使用我自己的数据,但这些值是数字......让我们假设我正在使用 R 附带的 mtcars 数据集.

Right now I am using my own data, but the values are numeric...let's just pretend I am using the mtcars dataset that comes with R.

我想:1. 允许我的用户通过下拉菜单从数据框中选择数字变量

I would like to: 1. allow my user to select a numeric variable from a dataframe via a drop down menu

  1. 让计算机计算所述变量的最小值和最大值

  1. have the computer calculate the minimum and maximum values for said variable

允许我的用户从滑块输入中输入 1 到 50 之间的数字

allow my user to input a number between 1 and 50 from a slider input

将第一个变量的最大值和最小值之差除以从滑块输入中获得的值

Divide the difference between the maximum and minimum of the first variable by the value obtained from the slider input

我得到的第一个错误是:1. 二元运算符的非数字参数

The first error I got was: 1. Non-numeric argument to binary operator

所以我尝试使用 as.numeric() 来强制变量,但这导致了我的第二个错误:

So I tried to coerce the variable using as.numeric(), but this led to my second error:

  1. 强制引入的 NA

在我的 server.R 文件中放置一个 print(max(input$Var) 行会为我提供变量名称而不是数字.

Putting a print(max(input$Var) line in my server.R file gives me the variable name instead of a number.

对此的任何帮助将不胜感激.我已经在下面发布了我的 ui.r 和 server.r 文件的一部分.先感谢您.

Any help with this would be greatly appreciated. I have posted part of my ui.r and server.r files below. Thank you in advance.

ui.R:

library(shiny)
load('mtcars')
shinyUI(pageWithSidebar(  
  headerPanel("MtCars Interactive Analysis Tool"),
  sidebarPanel(
selectInput(inputId = "histVar",
label= "Pick Your Variable",
choices=names(mtcars),
selected = NULL)),

sliderInput(inputId = "binNum",
label = h5("Number of bins for Histogram--1 through 50 are allowed"),
min = 1,
max = 50,
value = 15, 
step = 1)

mainpanel(...blah blah blah) 

server.R:

library(shiny)
load("mtcars")


shinyServer(function(input, output) {

output$histPlot <- renderPlot({

  varRangeGet <- range(input$histVar)
  #print(varRangeGet)
  #print(class(varRangeGet))
  #print(max(input$histVar))
  realRange<-(varRangeGet[[2]]- varRangeGet[[1]])
  binwidthX <- realRange/input$binNum

# Errors out...can't continue

})


})

再次感谢您对此的任何帮助...我不明白为什么我不能在 Shiny 中对输入变量执行简单计算....

Again, thank you for any help on this...I don't understand why I can't perform simple calculations on an input variable within Shiny....

推荐答案

我喜欢从简单开始,所以这里将您的代码修改为单个脚本.首先看看你能不能做到这一点.在我的系统上工作.我从未见过有人在 ui.R 代码中加载数据.如果您需要动态下拉下拉菜单,则需要使用 uiOutput,在 server.R 中加载数据,然后在 ui 中呈现它.无论如何,首先尝试运行此代码.

I like to start out simple so here is your code modified into a single script. First see if you can get this going. Works on my system. I have never seen anyone load data in the ui.R code. If you need to make your drop down dynamic then you need to utilize uiOutput, load data in server.R and then render it in the ui. Anyway first try running this code.

library(shiny)
runApp(list(
  ui = pageWithSidebar(  
    headerPanel("MtCars Interactive Analysis Tool"),
    sidebarPanel(
      selectInput(inputId = "histVar",
                  label= "Pick Your Variable",
                  choices=names(mtcars),
                  selected = NULL)),

    sliderInput(inputId = "binNum",
                label = h5("Number of bins for Histogram--1 through 50 are allowed"),
                min = 1,
                max = 50,
                value = 15, 
                step = 1)),
  server = function(input, output) {
    output$histPlot <- renderPlot({

    varRangeGet <- range(input$histVar)
    #print(varRangeGet)
    #print(class(varRangeGet))
    #print(max(input$histVar))
    realRange<-(varRangeGet[[2]]- varRangeGet[[1]])
    binwidthX <- realRange/input$binNum

    # Errors out...can't continue

  })
  }
),port = 3300)

这篇关于获取 input$variable Shiny RStudio 的最大值和最小值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆