Question: I have a dataframe in R of the following form: 

```
                     lemma       mi                                 doc
1        butroe_polihalino 5.385031 ANEJO_05_Q ECOL_Cantabrico Oriental
2        tramo_oligohalino 5.385031 ANEJO_05_Q ECOL_Cantabrico Oriental
3 programas_de_seguimiento 5.385031 ANEJO_05_Q ECOL_Cantabrico Oriental
4                     ifim 5.385031 ANEJO_05_Q ECOL_Cantabrico Oriental
5                      rdl 5.385031 ANEJO_05_Q ECOL_Cantabrico Oriental
6                 prointec 5.385031 ANEJO_05_Q ECOL_Cantabrico Oriental

```

And I want to create a sparse matrix (in which I'll run PCA) of the form:

```
                butroe_polihalino tramo_oligohalino ...
ANEJO_05_...    5.385031          5.385031
docs2           (no value)        4.321345
...

```

Which basically contains docs as rows, lemmas as columns and the mi is the value. Also it is possible to have not defined values.

Is there any way of defining a sparse matrix like that in R from the data?

Answer: 
I'm having same scenario, this worked for me but i'm not having the "hide/show" feature you have. So perhaps you could first check if you get the focus when you have the field always visible, and then try to solve why does not work when you change visibility (probably that's why you need to apply a sleep or a promise)

To set focus, this is the only change you need to do:

your Html mat input should be:

```
<input #yourControlName matInput>

```

in your TS class, reference like this in the variables section (

```
export class blabla...
    @ViewChild("yourControlName") yourControl : ElementRef;

```

Your button it's fine, calling:

```
  showSearch(){
       ///blabla... then finally:
       this.yourControl.nativeElement.focus();
}

```

and that's it.
You can check this solution on this post that I found, so thanks to -->
<https://codeburst.io/focusing-on-form-elements-the-angular-way-e9a78725c04f>
Easier way is also to do this.

```
let elementReference = document.querySelector('<your css, #id selector>');
    if (elementReference instanceof HTMLElement) {
        elementReference.focus();
    }

```