r - R Shiny,根据用户上传的文件创建反应图
问题描述
我正在开发一个应用程序(除其他外) 1. 允许用户上传 csv 数据集(这似乎可行) 2. 根据用户在单独的选项卡中选择的文件绘制热图
该应用程序似乎正在运行,但是我无法获取热图来绘制用户选择的数据,我认为它需要以某种方式进行响应,但我无法调用上传的文件。
到目前为止我的代码
UI:图书馆(闪亮)
shinyUI(fluidPage(
tabsetPanel(
tabPanel("File Input",
sidebarLayout(
sidebarPanel(
fileInput("file","Upload the File"),
tags$hr(),
h5(helpText("Select the read.table parameters below")),
checkboxInput(inputId = 'header', label = 'Header', value = FALSE),
checkboxInput(inputId = "stringAsFactors", "stringAsFactors", FALSE),
br(),
radioButtons(inputId = 'sep', label = 'Separator', choices = c(Comma=',',Semicolon=';',Tab='\t', Space=''), selected = ',')
),
mainPanel(
uiOutput("tb")
)
)),
tabPanel("Heatmap",
plotOutput('heatmap', width = "100%", height = "500px")
))
))
服务器
library(shiny)
shinyServer(function(input,output,session){
data <- reactive({
file1 <- input$file
if(is.null(file1)){return()}
read.table(file=file1$datapath, sep=input$sep, header = input$header, stringsAsFactors = input$stringAsFactors)
})
output$filedf <- renderTable({
if(is.null(data())){return ()}
input$file
})
output$sum <- renderTable({
if(is.null(data())){return ()}
summary(data())
})
output$table <- renderTable({
if(is.null(data())){return ()}
data()
})
output$tb <- renderUI({
if(is.null(data()))
h5("Powered by copious amounts of coffee", tags$img(src='coffee.png', heigth=200, width=200))
else
tabsetPanel(tabPanel("About file", tableOutput("filedf")),tabPanel("Data", tableOutput("table")),tabPanel("Summary", tableOutput("sum")))
})
mps<-reactive({
if(is.null(file_to_read)){
return()
}
plots<-reactive({
output$heatmap <- renderPlot({ heatmap(data, main = "heatmap")
})})
})})
有没有办法将数据命名为要在热图中使用的对象,例如
mat<-as.matrix(user uploaded file, row.numbers=1)
output$heatmap <- renderPlot({ heatmap(mat, main = "heatmap")
解决方案
@Niall - 这是我必须创建的热图。希望这会有所帮助。我mtcars
在 .csv 文件中对其进行了测试:
"mpg","cyl","disp","hp","drat","wt","qsec","vs","am","gear","carb"
"Mazda RX4",21,6,160,110,3.9,2.62,16.46,0,1,4,4
"Mazda RX4 Wag",21,6,160,110,3.9,2.875,17.02,0,1,4,4
"Datsun 710",22.8,4,108,93,3.85,2.32,18.61,1,1,4,1
...
在header
闪亮的应用程序中选择选项。
library(shiny)
ui <- fluidPage(
tabsetPanel(
tabPanel("File Input",
sidebarLayout(
sidebarPanel(
fileInput("file","Upload the File"),
tags$hr(),
h5(helpText("Select the read.table parameters below")),
checkboxInput(inputId = 'header', label = 'Header', value = FALSE),
checkboxInput(inputId = "stringAsFactors", "stringAsFactors", FALSE),
br(),
radioButtons(inputId = 'sep', label = 'Separator', choices = c(Comma=',',Semicolon=';',Tab='\t', Space=''), selected = ',')
),
mainPanel(
uiOutput("tb")
)
)),
tabPanel("Heatmap",
plotOutput('heatmap', width = "100%", height = "500px")
))
)
server <- function(input,output,session){
data <- reactive({
file1 <- input$file
if(is.null(file1)){return()}
read.table(file=file1$datapath, sep=input$sep, header = input$header, stringsAsFactors = input$stringAsFactors)
})
output$filedf <- renderTable({
if(is.null(data())){return ()}
input$file
})
output$sum <- renderTable({
if(is.null(data())){return ()}
summary(data())
})
output$table <- renderTable({
if(is.null(data())){return ()}
data()
})
output$tb <- renderUI({
if(is.null(data()))
h5("Powered by copious amounts of coffee", tags$img(src='coffee.png', heigth=200, width=200))
else
tabsetPanel(tabPanel("About file", tableOutput("filedf")),tabPanel("Data", tableOutput("table")),tabPanel("Summary", tableOutput("sum")))
})
output$heatmap <- renderPlot({
if (is.null(data())) {
return()
}
heatmap(as.matrix(data()), main = "heatmap")
})
}
shinyApp(ui = ui, server = server)
推荐阅读
- string - 带有 ord 'x' 的 Haskell 案例陈述
- facebook - 尝试在 Facebook 应用程序上将用户添加为测试人员时出错
- vim - 通过绑定在 Vim 中插入选项卡只能使用一次
- c# - 场
已分配,但从未使用过它的值 - php - 如何在php循环中查询rand的结果?
- linker - 可以将导入绑定在 64 位可执行文件中吗?
- python - 如何以特定方式格式化浮点数?
- java - 如何在java中恢复h2数据库
- c++ - 自引用类
- azure-web-app-service - 如何在不同目录中使用 Azure Key Vault 和 Azure Active Directory B2C?