R 如何在Shining中从下拉框中选择变量时动态创建直方图
我想创建一个闪亮的应用程序,用户将上传一个csv文件,上传后下拉框将填充数据集中的所有变量 当用户从下拉框中选择一个变量时,应在主面板中绘制相应的直方图 这就是我所做的 ui.r文件R 如何在Shining中从下拉框中选择变量时动态创建直方图,r,shiny,R,Shiny,我想创建一个闪亮的应用程序,用户将上传一个csv文件,上传后下拉框将填充数据集中的所有变量 当用户从下拉框中选择一个变量时,应在主面板中绘制相应的直方图 这就是我所做的 ui.r文件 library(shiny) shinyUI(pageWithSidebar( headerPanel( "Demand Forecast", "Flowserve"), sidebarPanel( fileInput('file1', 'Select csv file',accept=c('text/csv')
library(shiny)
shinyUI(pageWithSidebar(
headerPanel( "Demand Forecast", "Flowserve"),
sidebarPanel(
fileInput('file1', 'Select csv file',accept=c('text/csv')
),
checkboxInput('header', 'Header', TRUE),
radioButtons('sep', 'Separator', c(Comma=',',Tab='\t', Semicolon=';' )
),
tags$hr(),
selectInput("product", "Select: ","")
),
mainPanel(tableOutput('contents'),
plotOutput("hist")
)
))
server.r文件
library(shiny)
library(ggplot2)
shinyServer(function(input,output,session){
dataUpload<-reactive({
inFile<-input$file1
print(inFile)
if(is.null(inFile))
return(NULL)
dt_frame = read.csv(inFile$datapath, header=input$header, sep=input$sep)
updateSelectInput(session, "product", choices = names(dt_frame))
})
output$hist <- renderPlot({
dataset<- dataUpload()
hist(dataset[,dataset$product])
})
})
库(闪亮)
图书馆(GG2)
shinyServer(功能(输入、输出、会话){
欢迎来到R闪亮的世界
- 您想将选定列用于直方图->
hist(数据集[,输入$product])
- 您的反应函数不返回任何内容,但您已将
数据集添加到反应函数return()
- 您还需要在
renderPlot()中检查错误
最简单的工作示例:
library(shiny)
library(ggplot2)
server <- function(input, output, session) {
dataUpload<-reactive({
inFile<-input$file1
print(inFile)
if(is.null(inFile))
return(NULL)
dt_frame = read.csv(inFile$datapath, header=input$header, sep=input$sep)
updateSelectInput(session, "product", choices = names(dt_frame))
return(dt_frame)
})
#output$contents <- renderTable({
# dataset<- dataUpload()
# dataset
#})
output$hist <- renderPlot({
# we need error check also here
if(is.null(input$file1))
return(NULL)
dataset<- dataUpload()
hist(dataset[,input$product])
})
}
ui <- pageWithSidebar(
headerPanel( "Demand Forecast", "Flowserve"),
sidebarPanel(
fileInput('file1', 'Select csv file',accept=c('text/csv')
),
checkboxInput('header', 'Header', TRUE),
radioButtons('sep', 'Separator', c(Comma=',',Tab='\t', Semicolon=';' )
),
tags$hr(),
selectInput("product", "Select: ","")
),
mainPanel(tableOutput('contents'),
plotOutput("hist")
)
)
shinyApp(ui = ui, server = server)
库(闪亮)
图书馆(GG2)
服务器这是一个很好的例子,说明了为什么不使用函数名作为对象名—一个原因是它可能会导致错误消息混淆。dt
是一个以R为基数的函数(请参见?dt
),当您调用dt[,colm]
如果作用域中不存在数据框dt
,R将尝试对函数dt
进行子集设置。因此,请查看您是否能够找出数据框dt
不在hist
调用范围内的原因。@jbaums感谢您的rpl。.我现在运行此应用程序时已更改了我的server.R文件。它给我一个错误:X必须b numeric是传递给hist()
函数numeric?@Keniajin No.的值。实际上我已经直接从下拉框中提取了变量。因此,它还包含其他类型的变量。