如何在 Shiny 中使用 textInput 来命名上传数据框的列

安德鲁·科林

我试图找到一种方法来使用 textInput 字段来允许用户在 Shiny 中指示数据框的列名。仪表板上的一点背景:

仪表板允许用户上传一个 csv 文件并在他们想要使用的数据框中指明列。然后,他们选择列名和行名。然后他们上传的数据将被转换成一个较小的数据框,用于对应分析。

除了列/行命名方面,一切正常。出于某种原因,当程序运行时,它将文本输入指定为第一行/列的名称,然后将其他名称命名为“NA”。我尝试使用 strsplit() 来分解文本输入,但这不起作用。

有人能告诉我我做错了什么吗?这是我的代码和问题的样子:

ui <- fluidPage(
  # Application title
  titlePanel("Perceptual Map Dashboard"),
  sidebarLayout(
# Sidebar with a slider and selection inputs
    sidebarPanel(

    #Excel doc row and column names
      numericInput(inputId="startcol",label="Input start column:",value="", min=1,max=10000),
      numericInput(inputId="endcol",label="Input end column:",value="", min=1,max=10000),
    #Inputing brands and emotions
      br(),
      numericInput(inputId = "rownums",label = "How many emotions are you evaluating?",value = "", min = 1,max = 10000),
      br(),
      textInput ( 'brands', 'List the brands included in your perceptual map (separated by commas):', value=""),
      textInput ( 'emotions', 'List the emotions included in your perceptual map (separated by commas):', value=""),
    #Removing brands and emotions

    #Select graph type
      textInput(inputId="plottitle",label="Title your graph:"),
    #Upload Excel Grid
      fileInput(inputId = 'data', 'Upload CSV File',
                accept=c('.csv')),
    actionButton("go","Create Map")
    ),

# Visual Output
    mainPanel(
      wellPanel(h4('Visual')),
      plotOutput(outputId = "plot",  width = "100%", height=500), 
      downloadButton("downloadPlot", "Download Visual")
        )
      )
    )



    server <- function(input,output){



      observeEvent(input$go,{

    x <- read.csv(input$data$datapath, header = F)
    print(x)
    str(x)


    plot1 <- reactive({
    x<-x[,as.numeric(input$startcol):as.numeric(input$endcol)]
    column.sums<-colSums(x)
    print(column.sums)
    pmd.matrix<-matrix(column.sums, byrow = T, nrow=as.numeric(input$rownums))
    pmd.df2<-as.data.frame(pmd.matrix)
    # colnames(pmd.df2) = names
    # print(pmd.df2)
    # # row.names(pmd.df2)= c(as.character(input$emotions))
    print(pmd.df2)
    fit <- CA(pmd.df2, graph=F)
    K <- plot.CA(fit, col.row = "blue", col.col="black",  cex=.6,new.plot=T,
                 title=input$plottitle)
    return(K)





        })

    output$plot<- renderPlot({

          print(plot1())

      })

    output$downloadPlot2 <- downloadHandler(
      filename = "Shinyplot.png",
      content = function(file) {
        png(file)
        print(plot1)
        dev.off()
      })



     })

    }



    shinyApp(ui = ui, server = server)


ISSUE I AM SEEING: 

       c("“Burlington”", "”JC Penny”", "”Kohls”", "”TJ Maxx”", "”Marshalls”", "”Nordstrom Rack”", "”Old Navy”", "”Target”", "”Walmart”", "”Macys”", "”Amazon”", "”Nordstrom”", "”Ross”")
    1                                                                                                                                                                                113
    2                                                                                                                                                                                113
    3                                                                                                                                                                                 69
    4                                                                                                                                                                                 55
    5                                                                                                                                                                                 91
    6                                                                                                                                                                                 73
    7                                                                                                                                                                                106
    8                                                                                                                                                                                 77
    9                                                                                                                                                                                 76
    10                                                                                                                                                                                80
    11                                                                                                                                                                                58
    12                                                                                                                                                                                86
        NA  NA NA NA NA NA NA  NA  NA NA  NA NA
    1   63  78 87 68 67 33 57  67  87 96  77 41
    2  123 100 71 70 50 77 67  93  33 44  35 36
    3   93  84 68 56 79 44 41  41 129 40 132 38
    4   43  51 62 63 59 50 58 109  70 82  66 27
    5  105  91 65 79 51 70 49  67  40 56  34 36
    6   69  71 89 66 55 68 83  51  39 67  34 44
    7   53  61 46 59 53 76 53  80  39 65  32 30
    8   55  55 52 61 63 48 85  70  57 97  60 32
    9   66  68 71 72 71 47 52  57  89 61  94 48
    10  61  55 60 56 52 56 39  60  37 39  33 65
    11  69  66 56 56 40 63 69  68  27 65  27 69
    12  70  66 59 61 46 73 62  72  47 86 113 63
索伦

看起来可能有两个问题。首先,我假设列名是在 UI 的“品牌”中定义的。在示例中,命名被注释掉了,但我创建了一个最小的示例,我认为它重新创建了您所看到的内容。

1) 将“品牌”定义为以逗号分隔的字符串作为您的 textInput 请求

brands<- "b1,b2,b3"

2)另一个问题是“名称”变量没有被定义?也许这是由于注释掉。我假设它需要是“名称 <- input$brands”。在这里,在闪亮的环境之外:

names <- brands

3) 用最小的 data.frame 'x' 重新创建过程

x<-data.frame(a=c(1,2,3),b=c(1,2,3),c=c(4,3,2))
pmd.matrix <- matrix(colSums(x),byrow=T,nrow=1)
pmd.df2 <- as.data.frame(pmd.matrix)

colnames(pmd.df2) <- names
pmd.df2

pmd.df2 的输出显示第一列已命名,其他列如您的示例中所示为 NA

> pmd.df2 b1,b2,b3 NA NA 1 6 6 9

原因是 input$names 是一个字符串变量,所以只有一个值。它需要拆分。下面使用 strsplit() 将返回一个 list() 对象,该对象可以是 unlist() 以返回要命名的对象向量,其修复如下:

names <- unlist(strsplit(brands,","))
colnames(pmd.df2) <- names
pmd.df2

并返回命名列的结果:

> pmd.df2 b1 b2 b3 1 6 6 9

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何在R Shiny中添加更大的textInput框?

来自分类Dev

如何在R Shiny中添加更大的textInput框?

来自分类Dev

您如何计算从Shiny中的textInput框中获取的数据?

来自分类Dev

如何在 ui.R 中读取 TextInput,在 global.R 中使用此值处理查询并使用 Shiny 在 server.R 中显示

来自分类Dev

如何在Shiny Dashboard中更改textInput的标签颜色

来自分类Dev

如何在Shiny中使用查询参数

来自分类Dev

R Shiny:如何基于textInput进行操作

来自分类Dev

如何在Kivy TextInput中使用.svg图形?

来自分类Dev

如何在R Shiny中使用不同的输入类型来触发observeEvent和action?

来自分类Dev

R Shiny:如何在服务器部分中使用来自ui的UI文本输入来设置数据帧?

来自分类Dev

如何使用insertUI在Shiny应用程序中将textInput转换为输出

来自分类Dev

如何在Shiny中使数据集具有反应性?

来自分类Dev

如何使用Shiny绘制上传的数据集?

来自分类Dev

如何在以数字命名的数据框中使用for循环

来自分类Dev

如何在Shiny中使用Javascript更改音量?

来自分类Dev

如何在R Shiny中使用sjPlot报告html表?

来自分类Dev

Shiny:标签位置,textInput

来自分类Dev

如何确保Shiny中的textInput只接受数字值?

来自分类Dev

R Shiny:在函数内使用HTML(例如textInput,checkboxGroupInput)

来自分类Dev

Python:如何在数据框中使用动态列命名将列拆分为多个列

来自分类Dev

在Shiny中使用反应性数据框进行预测(SVM)

来自分类Dev

尝试在Shiny中使用renderDataTable显示数据框时出错

来自分类Dev

使用R Shiny上传后编辑数据框

来自分类Dev

如何在React Native中使用React钩子使用TextInput更改对象数组的特定字符串?

来自分类Dev

如何使用R Shiny筛选数据

来自分类Dev

如何在python中使用模拟数据创建数据框

来自分类Dev

如何在Shiny应用程序中使用ggplot创建反应式图

来自分类Dev

如何在R Shiny应用程序中使用{gtsummary}软件包

来自分类Dev

如何在服务器 Shiny 中使用反应变量

Related 相关文章

  1. 1

    如何在R Shiny中添加更大的textInput框?

  2. 2

    如何在R Shiny中添加更大的textInput框?

  3. 3

    您如何计算从Shiny中的textInput框中获取的数据?

  4. 4

    如何在 ui.R 中读取 TextInput,在 global.R 中使用此值处理查询并使用 Shiny 在 server.R 中显示

  5. 5

    如何在Shiny Dashboard中更改textInput的标签颜色

  6. 6

    如何在Shiny中使用查询参数

  7. 7

    R Shiny:如何基于textInput进行操作

  8. 8

    如何在Kivy TextInput中使用.svg图形?

  9. 9

    如何在R Shiny中使用不同的输入类型来触发observeEvent和action?

  10. 10

    R Shiny:如何在服务器部分中使用来自ui的UI文本输入来设置数据帧?

  11. 11

    如何使用insertUI在Shiny应用程序中将textInput转换为输出

  12. 12

    如何在Shiny中使数据集具有反应性?

  13. 13

    如何使用Shiny绘制上传的数据集?

  14. 14

    如何在以数字命名的数据框中使用for循环

  15. 15

    如何在Shiny中使用Javascript更改音量?

  16. 16

    如何在R Shiny中使用sjPlot报告html表?

  17. 17

    Shiny:标签位置,textInput

  18. 18

    如何确保Shiny中的textInput只接受数字值?

  19. 19

    R Shiny:在函数内使用HTML(例如textInput,checkboxGroupInput)

  20. 20

    Python:如何在数据框中使用动态列命名将列拆分为多个列

  21. 21

    在Shiny中使用反应性数据框进行预测(SVM)

  22. 22

    尝试在Shiny中使用renderDataTable显示数据框时出错

  23. 23

    使用R Shiny上传后编辑数据框

  24. 24

    如何在React Native中使用React钩子使用TextInput更改对象数组的特定字符串?

  25. 25

    如何使用R Shiny筛选数据

  26. 26

    如何在python中使用模拟数据创建数据框

  27. 27

    如何在Shiny应用程序中使用ggplot创建反应式图

  28. 28

    如何在R Shiny应用程序中使用{gtsummary}软件包

  29. 29

    如何在服务器 Shiny 中使用反应变量

热门标签

归档